package com.remon.quartz.controller;

import com.remon.quartz.controller.restfule.Response;
import com.remon.quartz.entity.Customer;
import com.remon.quartz.service.CustomerService;
import com.remon.quartz.vo.CustomerReq;
import github.remonlong.restfulutil.exception.RestfulResultUtil;
import github.remonlong.restfulutil.exception.Result;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * jwt 签名获取
 *
 * @author remon
 * @create 2018-08-16 0:15
 **/
@RestController
@Slf4j
public class JwtController {

    @Autowired
    private CustomerService customerService;

    /**
     * 生成token
     *
     * @param appid  appid
     * @param secret appsecret
     * @return
     * @throws Exception
     */
    @GetMapping("genToken/{appid}/{secret}")
    @ResponseBody
    public Result<String> genToken(@PathVariable String appid, @PathVariable String secret) throws Exception {
        Customer customer = customerService.findByAppid(CustomerReq.builder().appid(appid).appsecret(secret).build());
        //加密生成token
        String token = Jwts.builder().setSubject(appid)
                .claim("roles", "all").setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256, customer.getAppsecret()).compact();
        return RestfulResultUtil.success(token);
    }

}
